"""
Author: Morphlng
Date: 2024-03-14 22:20:04
LastEditTime: 2024-03-21 10:50:01
LastEditors: Morphlng
Description: Stub for carla module. Allowing the rest of the code to run without carla.
FilePath: /DrivingGym/src/driving_gym/simulation/common/stub.py
"""


class _FakeCarlaClassStub:
    def __init__(self, *args, **kwargs):
        raise ImportError(f"Could not import `carla`.")


class _FakeCarlaStub:
    def __init__(self, *args, **kwargs):
        self.World = _FakeCarlaClassStub
        self.Map = _FakeCarlaClassStub
        self.Transform = _FakeCarlaClassStub
        self.Location = _FakeCarlaClassStub
        self.Rotation = _FakeCarlaClassStub
        self.Vector3D = _FakeCarlaClassStub
        self.Waypoint = _FakeCarlaClassStub
        self.Actor = _FakeCarlaClassStub
        self.Vehicle = _FakeCarlaClassStub
        self.Sensor = _FakeCarlaClassStub
        self.LaneChange = _FakeCarlaClassStub
        self.LaneType = _FakeCarlaClassStub
        self.LaneMarking = _FakeCarlaClassStub


def try_import_carla(error: bool = False):
    try:
        import carla
    except ImportError:
        if error:
            raise ImportError(
                f"Could not import `carla`. HDMap related functions are not available."
            )
        carla = _FakeCarlaStub()
    return carla
